/**
 * Connect to MongoDB via mongoose driver for non-file operation purpose
 */
//=====================================================================
global.mongoose = require('mongoose');

//DAYU  var mongoUri = mongoUri;
var mongoUri = global.mongoUri;
mongoose.connect(mongoUri);

//global.MongoUri = 'mongodb://120.55.118.234:27017/66xiaoqu,mongodb://120.55.118.244:27017/66xiaoqu'

global.db = mongoose.connection;

db.on('connected', function () {
    logger.info('Connected to MongoDB via mongoose with - ' + mongoUri + '...!');

});

db.on('error', function (err) {
    logger.info('unable to connect to database at ' + mongoUri + ':' + err.message);
});


/**
 * Load and initiate all of the scheams of mongoDB
 */
require('../services/models/mongodb/user-mongo');
require('../services/models/mongodb/group-mongo');
require('../services/models/mongodb/question-mongo');
require('../services/models/mongodb/community-mongo');
require('../services/models/mongodb/relationShip-mongo');
require('../services/models/mongodb/masterData-mongo');
require('../services/models/mongodb/system-mongo');
require('../services/models/mongodb/activity-mongo');
require('../services/models/mongodb/market-mongo');
require('../services/models/mongodb/task-mongo');

require('../services/models/mongodb/backoffice/backOffice-audit-mongo');
require('../services/models/mongodb/backoffice/backOffice-rest-mongo');
require('../services/models/mongodb/backoffice/backOffice-adminUser-mongo');
require('../services/models/mongodb/backoffice/backOffice-lottery-mongo');
require('../services/models/mongodb/backoffice/backOffice-userServiceChain-mongo');
require('../services/models/mongodb/userDeviceProfile-mongo');

require('../services/models/mongodb/mobile/shares-mongo');

// require('../services/models/mongodb/weixin/wish-mongo');

require('../services/models/mongodb/parking-mongo');
require('../services/models/mongodb/mtOrder-mongo');
/**
 * Connection to MySQL
 */
//=====================================================================
if (MySQLFlag == 'Yes') {
    var mysql = require('mysql');
    //global.mysqlClient = mysql.createConnection({
    //    host: MySQLHostIp,
    //    user: 'root',
    //    password: '123456'
    //});
    //mysqlClient.connect(function (err, results) {
    //    if (err) {
    //        logger.info('Connect to mySQL database failed:' + err.message);
    //    } else {
    //        logger.info('Connected to mySQL database - ' + host + '!');
    //    }
    //});

    global.mysqlPool = mysql.createPool(mysqlPoolOption);

    mysqlPool.on('connection', function (connection) {
    });

    mysqlPool.on('enqueue', function () {
        //console.log('Waiting for available connection slot');
    });


    global.mysqlLogPool = mysql.createPool(mysqlPoolOption);
    global.mysqlRecPool = mysql.createPool(mysqlPoolOption);
}


